package com.lai.hospital.controller;


import com.lai.hospital.dto.UserBackDTO;
import com.lai.hospital.annotation.AccessLimit;
import com.lai.hospital.dto.UserAreaDTO;
import com.lai.hospital.dto.UserBackDTO;
import com.lai.hospital.dto.UserInfoDTO;
import com.lai.hospital.service.UserAuthService;
import com.lai.hospital.vo.*;
import com.lai.hospital.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户账号控制器
 *
 * @author yezhiqiu
 * @date 2021/07/28
 */
@Api(tags = "用户账号模块")
@RestController
public class UserAuthController {
    @Autowired
    private UserAuthService userAuthService;

    /**
     * 发送邮箱验证码
     *
     * @param username 用户名
     * @return {@link Result<>}
     */
    @AccessLimit(seconds = 60, maxCount = 1)
    @ApiOperation(value = "发送邮箱验证码")
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
    @GetMapping("/users/code")
    public Result<?> sendCode(String username) {
        userAuthService.sendCode(username);
        return Result.ok();
    }

    /**
     * 发送手机短信验证码
     *
     * @param phone 手机号
     * @return {@link Result<>}
     */
    @AccessLimit(seconds = 60, maxCount = 1)
    @ApiOperation(value = "发送手机短信验证码")
    @GetMapping("/user/sms")
    public Result<?> sendPhoneCode(@RequestParam("phone") String phone,@RequestParam("propose") Integer propose) {
        try {
            userAuthService.sendPhoneCode(phone,propose);
            return Result.ok();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }



    /**
     * 获取用户区域分布
     *
     * @param conditionVO 条件
     * @return {@link Result<UserAreaDTO>} 用户区域分布
     */
    @ApiOperation(value = "获取用户区域分布")
    @GetMapping("/admin/users/area")
    public Result<List<UserAreaDTO>> listUserAreas(ConditionVO conditionVO) {
        return Result.ok(userAuthService.listUserAreas(conditionVO));
    }

    /**
     * 查询后台用户列表
     *
     * @param condition 条件
     * @return {@link Result<UserBackDTO>} 用户列表
     */
    @ApiOperation(value = "查询后台用户列表")
    @GetMapping("/admin/users")
    public Result<PageResult<UserBackDTO>> listUsers(ConditionVO condition) {
        return Result.ok(userAuthService.listUserBackDTO(condition));
    }

    /**
     * 用户注册
     *
     * @param user 用户信息
     * @return {@link Result<>}
     */
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public Result<?> register(@Valid @RequestBody UserVO user) {
        userAuthService.register(user);
        return Result.ok();
    }

    /**
     * 修改密码
     *
     * @param user 用户信息
     * @return {@link Result<>}
     */
    @ApiOperation(value = "修改密码")
    @PutMapping("/users/password")
    public Result<?> updatePassword(@Valid @RequestBody UserVO user) {
        userAuthService.updatePassword(user);
        return Result.ok();
    }

    /**
     * 修改管理员密码
     *
     * @param passwordVO 密码信息
     * @return {@link Result<>}
     */
    @ApiOperation(value = "修改管理员密码")
    @PutMapping("/admin/users/password")
    public Result<?> updateAdminPassword(@Valid @RequestBody PasswordVO passwordVO) {
        userAuthService.updateAdminPassword(passwordVO);
        return Result.ok();
    }

    /**
     * 修改手机号
     * @param userPhoneLoginVO 手机号信息
     * @return
     */
    @ApiOperation(value = "修改手机号")
    @PutMapping("/user/phone")
    public Result<?> updatePhone(@RequestBody @Valid UserPhoneLoginVO userPhoneLoginVO){
        userAuthService.updatePhone(userPhoneLoginVO);
        return Result.ok();
    }

    /**
     * 获取微信用户openId
     * @param code
     * @return
     */
    @ApiOperation(value = "获取微信用户openId")
    @GetMapping("/user/oauth/getWxUserOpenId")
    public Result<?> getWxUserOpenId(String code){
        return Result.ok(userAuthService.getWxUserOpenId(code));
    }

    /**
     * 微信登录
     * @param weiXinLoginVO 微信登录信息
     * @return
     */
    @ApiOperation(value = "微信登录")
    @PostMapping("/user/oauth/wxLogin")
    public Result<?> wxLogin(@RequestBody @Valid WeiXinLoginVO weiXinLoginVO){
        return Result.ok(userAuthService.wxLogin(weiXinLoginVO));
    }

    /**
     * 手机号登录
     * @param userPhoneLoginVO 手机号信息
     * @return
     */
    @ApiOperation(value = "手机号登录")
    @PostMapping("/user/phoneLogin")
    public Result<?> phoneLogin(@RequestBody @Valid UserPhoneLoginVO userPhoneLoginVO){
        return Result.ok(userAuthService.phoneLogin(userPhoneLoginVO));
    }

//    /**
//     * 微博登录
//     *
//     * @param weiBoLoginVO 微博登录信息
//     * @return {@link Result<UserInfoDTO>} 用户信息
//     */
//    @ApiOperation(value = "微博登录")
//    @PostMapping("/users/oauth/weibo")
//    public Result<UserInfoDTO> weiboLogin(@Valid @RequestBody WeiboLoginVO weiBoLoginVO) {
//        return Result.ok(userAuthService.weiboLogin(weiBoLoginVO));
//    }
//
//    /**
//     * qq登录
//     *
//     * @param qqLoginVO qq登录信息
//     * @return {@link Result<UserInfoDTO>} 用户信息
//     */
//    @ApiOperation(value = "qq登录")
//    @PostMapping("/users/oauth/qq")
//    public Result<UserInfoDTO> qqLogin(@Valid @RequestBody QQLoginVO qqLoginVO) {
//        return Result.ok(userAuthService.qqLogin(qqLoginVO));
//    }

}

